const mongoose = require("mongoose")
mongoose.connect('mongodb://localhost:27017/test', {useUnifiedTopology: true, useNewUrlParser: true});

const studentSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true //必填
    },
    gender: {
        type: Number,
        required: true ,//必填
        enum:[0,1]
    },
    age: {
        type: Number,
        required: true //必填
    },
    hobbies: {
        type: String,
        required: true //必填
    },
})

const Student = mongoose.model('Student', studentSchema);

exports.add = function (student, callBack) {
    let stu = new Student(student)
    stu.save().then(res=>{
        callBack()
    })
}


exports.getById = function (id, callBack) {
    Student.findOne({_id:id},function (error,res) {
        callBack(res)
    })
}


exports.update = function (editStudent, callBack) {
    let _id = editStudent.id
    editStudent.id =null
    Student.updateOne({_id:_id},editStudent,function (error,res) {
        callBack()
    })
}

exports.deleteById = function (delId, callBack) {
    Student.deleteOne({_id:delId},function (error) {
        if (!error){
            callBack()
        }
    })
}


exports.list = function (callBack) {
    Student.find(function (error, list) {
        if (!error) {
            callBack(list)
        }
    })
}